package cn.iocoder.yudao.module.bpm.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.form.BpmFormRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormSearchBeanDO;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import com.alibaba.fastjson.JSON;
import com.fhs.core.trans.vo.VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

/**
 * @ClassName BaseController
 * @Description TODO
 * @Author dingyongze
 * @Date 2024/10/12 11:46
 * @Version 1.0
 */
public abstract class BaseController<DO> {
    @Autowired
    private BeanSearcher beanSearcher;

    @GetMapping("/searchOne")
    public CommonResult<DO> searchOne(DO data) {
        SearchResult<DO> search = beanSearcher.search(getDoClass(), BeanUtil.beanToMap(data));
        List<DO> dataList = search.getDataList();
        if (dataList.size() == 0) {
            return success(null);
        } else {
            if (getDoClass().getName().equals(BpmFormSearchBeanDO.class.getName())) {
                BpmFormSearchBeanDO aDo = (BpmFormSearchBeanDO) dataList.get(0);
                aDo.setFieldList(JSON.parseArray(aDo.getFields()));
            }
            return success(dataList.get(0));
        }
    }

    @GetMapping("/searchList")
    public  CommonResult<List<DO>>  searchList(DO data) {
        SearchResult<DO> search = beanSearcher.search(getDoClass(), BeanUtil.beanToMap(data));
        List<DO> dataList = search.getDataList();
        if (dataList.size() == 0) {
            return success(null);
        } else {
            return success(dataList);
        }
    }

    /**
     * 获取DO
     *
     * @return
     */
    public Class<DO> getDoClass() {
        return (Class<DO>) ((java.lang.reflect.ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

}
